/*
 * Copyright (c) 2005, 2014 vacoor
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package org.vacoor.nothing.common.dataset.support;

import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.vacoor.nothing.common.util.StringUtils;
import org.vacoor.nothing.common.util.Throwables;

import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author vacoor
 */
public class JxlWriter {

    public static JxlWriter on(OutputStream out) {
        return new JxlWriter();
    }

    public JxlWriter writeHeaders() {
        return this;
    }

    public <E> void export(String sheetName, List<E> data, OutputStream out) {
        try {
            sheetName = StringUtils.hasText(sheetName) ? sheetName : "Sheet1" ;
            String[] captions = buildExcelCaptions(data);

            WritableWorkbook workbook = jxl.Workbook.createWorkbook(out);
            WritableSheet sheet = workbook.createSheet(sheetName, 0);
            // 日期格式化, 使用默认的就好了
            // jxl.write.WritableCellFormat dateFmt = new jxl.write.WritableCellFormat(new jxl.write.DateFormat("yyyy/MM/dd"));

            // 标题
            boolean hasCaptions = captions != null && captions.length > 0;
            if (hasCaptions) {
                for (int c = 0; c < captions.length; c++) {
                    Label label = new Label(c, 0, captions[c]);
                    sheet.addCell(label);
                }
                sheet.getSettings().setVerticalFreeze(1); // 冻结前一行, 参数为冻结前几行
            }
            /*
            CellView cellView = new CellView();
            cellView.setSize(100 * 256);
            cellView.setFormat(dateFmt);
            sheet.setColumnView(4, cellView);
            */

            // 行
            for (int r = 0; r < data.size(); r++) {
                E e = data.get(r);
                if (e == null) {
                    continue;
                }

                // 列
                Object[] values = buildExcelData(e);
                int rn = hasCaptions ? r + 1 : r;

                for (int c = 0; c < values.length; c++) {
                    Object value = values[c];
                    if (value == null) {
                        continue;
                    }

                    WritableCell cell;


                    if (value instanceof Boolean) {
                        cell = new jxl.write.Boolean(c, rn, (Boolean) value);
                    } else if (value instanceof Number) {
                        cell = new jxl.write.Number(c, rn, ((Number) value).doubleValue());
                    } else if (value instanceof Date) {
                        cell = new jxl.write.DateTime(c, rn, (Date) value);
                        // cell.setCellFormat(dateFmt);
                    } else if (value instanceof Calendar) {
                        cell = new jxl.write.DateTime(c, rn, ((Calendar) value).getTime());
                        // cell.setCellFormat(dateFmt);
                    } else {
                        cell = new Label(c, rn, values[c].toString());
                    }

                    sheet.addCell(cell);
                }
            }

            workbook.write();
            workbook.close();
            System.gc();
        } catch (Exception e) {
            throw Throwables.unchecked(e);
        }
    }

    private <E> String[] buildExcelCaptions(List<E> data) {
        return new String[0];
    }

    private <E> Object[] buildExcelData(E e) {
        return new Object[0];
    }
}
